package com.dhcy.mqtt.service.impl;

import com.dhcy.mqtt.config.ProjectConfig;
import com.dhcy.mqtt.dao.EquipmentMapper;
import com.dhcy.mqtt.dao.OnlineDataMapper;
import com.dhcy.mqtt.iden.IdenUtil;
import com.dhcy.mqtt.model.Equipment;
import com.dhcy.mqtt.model.OnlineData;
import com.dhcy.mqtt.par.ht.HandleDto;
import com.dhcy.mqtt.service.AsynService;
import com.dhcy.mqtt.util.DateUtil;
import com.dhcy.mqtt.util.RandomUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Async
@Transactional
public class AsynServiceImpl implements AsynService {

    @Autowired
    private ProjectConfig projectConfig;
    @Autowired
    private IdenUtil idenUtil;
    @Autowired
    private EquipmentMapper equipmentMapper;
    @Autowired
    private OnlineDataMapper onlineDataMapper;

    /*
    *注册标识
    * type 1 设备注册  2 数据注册
     */
    @Override
    public void regIden(HandleDto handleDto, Integer type) {
        String handle= null;
        if (1 == type){
            handle = projectConfig.getPrefix()+"/mqhl"+handleDto.getDeviceNumber();
            if (projectConfig.getEnable()){
                String modelName = "煤气化炉设备";
                if (!idenUtil.modelExist(projectConfig.getRegurl(),projectConfig.getSnmsusername(),projectConfig.getSnmspassword(),projectConfig.getPrefix(), modelName)) {
                    List<Map<String, Object>> modelList = new ArrayList<>();
                    modelList.add(idenUtil.initModelItem("deviceNumber", "设备编号"));
                    modelList.add(idenUtil.initModelItem("time", "创建时间"));
                    idenUtil.regModel(projectConfig.getRegurl(),projectConfig.getSnmsusername(),projectConfig.getSnmspassword(),projectConfig.getPrefix(), modelName, modelList, 3);
                }
                List<Map<String, Object>> idenDataList = new ArrayList<>();
                idenDataList.add(idenUtil.initItem("2000", "deviceNumber", handleDto.getDeviceNumber()));
                idenDataList.add(idenUtil.initItem("2001", "time", DateUtil.parseDateToStr(new Date())));
                idenUtil.regIden(projectConfig.getRegurl(),projectConfig.getSnmsusername(),projectConfig.getSnmspassword(),handle, modelName, idenDataList);
            }
            Equipment equipment = new Equipment();
            equipment.setNumber(handleDto.getDeviceNumber());
            equipment.setHandle(handle);
            equipment.setCreateTime(new Date());
            equipmentMapper.insert(equipment);
        }else if (2 == type){
            handle = projectConfig.getPrefix()+"/mqhl"+ RandomUtil.generateString(6);
            if (projectConfig.getEnable()){
                String modelName = "煤气化炉数据";
                if (!idenUtil.modelExist(projectConfig.getRegurl(),projectConfig.getSnmsusername(),projectConfig.getSnmspassword(),projectConfig.getPrefix(), modelName)) {
                    List<Map<String, Object>> modelList = new ArrayList<>();
                    modelList.add(idenUtil.initModelItem("deviceNumber", "设备编号"));
                    modelList.add(idenUtil.initModelItem("temperature", "温度"));
                    modelList.add(idenUtil.initModelItem("humidity", "湿度"));
                    modelList.add(idenUtil.initModelItem("co", "一氧化碳"));
                    modelList.add(idenUtil.initModelItem("h2s", "硫化氢"));
                    modelList.add(idenUtil.initModelItem("ch4", "甲烷"));
                    modelList.add(idenUtil.initModelItem("o2", "氧"));
                    modelList.add(idenUtil.initModelItem("time", "创建时间"));
                    idenUtil.regModel(projectConfig.getRegurl(),projectConfig.getSnmsusername(),projectConfig.getSnmspassword(),projectConfig.getPrefix(), modelName, modelList, 3);
                }
                List<Map<String, Object>> idenDataList = new ArrayList<>();
                idenDataList.add(idenUtil.initItem("2000", "deviceNumber", handleDto.getDeviceNumber()));
                idenDataList.add(idenUtil.initItem("2001", "temperature", handleDto.getTemperature()));
                idenDataList.add(idenUtil.initItem("2002", "humidity", handleDto.getHumidity()));
                idenDataList.add(idenUtil.initItem("2003", "co", handleDto.getCo()));
                idenDataList.add(idenUtil.initItem("2004", "h2s", handleDto.getH2s()));
                idenDataList.add(idenUtil.initItem("2005", "ch4", handleDto.getCh4()));
                idenDataList.add(idenUtil.initItem("2006", "o2", handleDto.getO2()));
                idenDataList.add(idenUtil.initItem("2007", "time", DateUtil.parseDateToStr(new Date())));
                idenUtil.regIden(projectConfig.getRegurl(),projectConfig.getSnmsusername(),projectConfig.getSnmspassword(),handle, modelName, idenDataList);
            }
            OnlineData onlineData = new OnlineData();
            BeanUtils.copyProperties(handleDto,onlineData);
            onlineData.setCreateTime(new Date());
            onlineData.setHandle(handle);
            onlineDataMapper.insert(onlineData);
        }
    }
}
